package com.corpgovernment.common.dataloader;

import com.corpgovernment.common.bo.ReplaceOrderInfo;
import com.ctrip.corp.obt.async.redis.RedisClientDelegate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service("replaceOrderDataloader")
@Slf4j
public class ReplaceOrderDataloader {

    @Resource
    private RedisClientDelegate redisClientDelegate;

    /**
     * 替换用户卡号-存入缓存
     */
    public Object saveReplaceOrderInfo(String key, ReplaceOrderInfo replaceOrderInfo, Integer expireTime) {
        redisClientDelegate.withCommandsClient(c -> {
            c.set(key, replaceOrderInfo, expireTime, TimeUnit.SECONDS);
        });
        return key;
    }

    /**
     * 替换用户卡号-获取缓存
     */
    public ReplaceOrderInfo getReplaceOrderInfo(String key) {
        try {
            return redisClientDelegate.withCommandsClient(c -> c.get(key), ReplaceOrderInfo.class);
        } catch (Exception ex) {
            log.info("替换用户卡号-获取缓存异常:{}", ex);
        }
        return null;
    }
}
